﻿using Cms.Core.Infrastructure.Dependency;
using Cms.Core.Infrastructure.Extension;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;

namespace Cms.Core.Infrastructure.Runtime.Session
{
    public class ClaimsCmsSession : ICmsSession
    {
        public virtual Guid? UserId
        {
            get {
                var userIdClaim = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == Type);
                if ((userIdClaim?.Value).IsNullOrEmpty())
                {
                    return null;
                }

                return userIdClaim.Value.AsTo<Guid>();
            }
        }


        public IPrincipalAccessor PrincipalAccessor { get; set; }

        private string Type { get; set; } = "jti";

       

        public ClaimsCmsSession(IPrincipalAccessor principalAccessor)
        {

            PrincipalAccessor = principalAccessor;
      
        }
    }
}
